package controller.welcome;

import controller.common.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import services.upms.AccountPermissionServices;
import services.upms.PermissionServices;
import wt.entity.upms.AccountPermission;
import wt.entity.upms.AccountPermissionExample;
import wt.entity.upms.Permission;
import wt.entity.upms.PermissionExample;
import wt.staticData.ShortData;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 功能:
 * 作者: 洪世成
 * 日期: 2017/9/9
 * 版权所有: admin
 */
@Controller
@RequestMapping("welcome/home")
public class HomeController extends BaseController {
    @Autowired
    PermissionServices permissionServices;
    @Autowired
    AccountPermissionServices accountPermissionServices;

    @RequiresPermissions("welcome:home:default")
    @RequiresUser
    @RequestMapping("index")
    public String index(Map map) throws Exception {

        if ("3878".equals(getLoginAccount().getId())){
            PermissionExample permissionExample = new PermissionExample();
            permissionExample.setOrderByClause( "ORDERS ");
            permissionExample.createCriteria().andTypeEqualTo(ShortData.s0);
            List<Permission> permissionList = permissionServices.selectByExample(permissionExample);

            map.put("menus", permissionList);
            return "welcome/home/index";
        }



        AccountPermissionExample accountPermissionExample = new AccountPermissionExample();
        accountPermissionExample.createCriteria().andAccountIdEqualTo(getLoginAccount().getId());

        List<AccountPermission> accountPermissionList = accountPermissionServices.selectByExample(accountPermissionExample);
        List<String> permissionIdList = new ArrayList<String>();
        for (AccountPermission accountPermission : accountPermissionList) {
            permissionIdList.add(accountPermission.getPermissionId());
        }

        PermissionExample permissionExample = new PermissionExample();
        permissionExample.setOrderByClause( "ORDERS ");
        permissionExample.createCriteria().andTypeEqualTo(ShortData.s0).andIdIn(permissionIdList);
        List<Permission> permissionList = permissionServices.selectByExample(permissionExample);



        map.put("menus", permissionList);

        if (getLoginAccount().getRole()!=null){
            map.put("role",getLoginAccount().getRole().getName());
        }


        if (getLoginAccount().getCompanyInfo()!=null){
            map.put("name",getLoginAccount().getCompanyInfo().getName());
        }else if(getLoginAccount().getStaffInfo()!=null){
            map.put("name",getLoginAccount().getStaffInfo().getName());
        }else {
            map.put("name",getLoginAccount().getExpertBase().getName());
        }


        return "welcome/home/index";
    }

    @RequiresPermissions("welcome:home:default")
    @RequestMapping("welcome")
    public String welcome() {
        return "welcome/home/welcome";
    }





}